const router = new VueRouter({
    routes:[
        {
            path:'/staffpanel/login',
            name:'staff.login',
            component:staff_login,
            meta:['all'],
        },
        {
            path:'/staffpanel',
            component: staff_layout,
            children:[
                {
                    path:'/',
                    name:'staff.index',
                    component:staff_index,
                    meta:['admin','employee'],
                },
                {
                    path:'event',
                    name:'staff.event.index',
                    component:staff_event_index,
                    meta:['admin'],
                },
                {
                    path:'event/:event_id/message',
                    name:'staff.event.message',
                    component:staff_event_message,
                    meta:['admin'],
                    props:true,
                },
                {
                    path:'event/create',
                    name:'staff.event.create',
                    component:staff_event_create,
                    meta:['admin'],
                },
                {
                    path:'event/:event_id/detail',
                    name:'staff.event.detail',
                    component:staff_event_detail,
                    props:true,
                    meta:['admin'],
                },
                {
                    path:'employee',
                    name:'staff.employee.index',
                    component:staff_employee_index,
                    meta:['admin'],
                },
                {
                    path:'employee/create',
                    name:'staff.employee.create',
                    component:staff_employee_create,
                    meta:['admin'],
                },
                {
                    path:'client',
                    name:'staff.client.index',
                    component:staff_client_index,
                    meta:['admin'],
                },
                {
                    path:'client/create',
                    name:'staff.client.create',
                    component:staff_client_create,
                    meta:['admin'],
                },
            ]
        },
        {
            path:'/userpanel',
            component:user_layout,
            children: [
                {
                    path:'/',
                    name:'user.index',
                    component:user_index,
                    meta:['all'],
                },
                {
                    path:'event',
                    name:'user.event.index',
                    component:user_event_index,
                    meta:['all'],
                },
                {
                    path:'event/:event_code/:event_phone/message',
                    name:'user.event.message',
                    component:user_event_message,
                    props:true,
                    meta:['all'],
                },
                {
                    path:'event/:event_code/preview',
                    name:'user.event.preview',
                    component:user_event_preview,
                    props:true,
                    meta:['all'],
                },
                {
                    path:'enroll/:event_id',
                    name:'user.enroll.index',
                    component:user_enroll_index,
                    props:true,
                    meta:['all'],
                },
                {
                    path:'enroll/enrollment',
                    name:'user.enroll.enrollment',
                    component:user_enroll_enrollment,
                    meta:['all'],
                },
            ]
        }
    ]
});


router.beforeEach((to,from,next)=>{
    console.log(to)
    let can_next = false;
    for (let role of to.meta){
        switch (role) {
            case 'all':
                can_next = true;
                break;
            case 'admin':
                if (!store.is_auth()) next({name:'staff.login'});
                if (store.auth.info.is_admin) can_next = true;
                break;
            case 'employee':
                if (!store.is_auth()) next({name:'staff.login'});
                if (!store.auth.info.is_admin) can_next = true;
                break;
        }
    }
    if (can_next){
        next()
    } else{
        Bus.$emit('m','You have not Permission');
    }
});
